package queped;

import java.util.HashMap;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Jason Ye
 * Date: 03/02/2012
 * Time: 17:36
 */
public class NeptuneHelperFunctions {

    public static double logBase2(double number) {
        return Math.log(number) / Math.log(2);
    }

    public static double MyMathRandom() {
        return Math.random() * 100;
    }

    public static double logBaseArbitrary(double base, double number) {
        return Math.log(number) / Math.log(base);
    }

    public static boolean isInDebugMode() {


        boolean isDebug = java.lang.management.ManagementFactory.
                getRuntimeMXBean().getInputArguments().toString().
                indexOf("-agentlib:jdwp") > 0;
        return isDebug;
    }

    /**
     * returns the GCD of n integers
     *
     * @param input
     * @return
     */
    public static int gcd(List<Integer> input) {
        int currentGCD = 0;
        for (Integer i : input) {
            currentGCD = gcd(i, currentGCD);
        }
        return currentGCD;
    }

    public static int gcd(int p, int q) {
        if (q == 0) {
            return p;
        }
        return gcd(q, p % q);
    }

    /**
     * modified sieve of Eros.
     *
     * @param n
     * @return
     */
    public static boolean isPrime(int n) {

        // sieve out 2
        if (n == 2) return true;

        // check if i == 1 or i is even
        if (n < 2 || (n & 0x1) == 0) return false;
        // check if i is divisible by anything else

        for (int i = 3; i * i <= Math.sqrt(n); i = i + 2) {
            if (n % i == 0) return false;
        }
        return true;
    }

    public int nextPrime(int n) {

        n = (n % 2 == 0) ? n + 1 : n + 2;
        while (!isPrime(n)) {
            n += 2;
        }
        return n;

    }

    public static <K, V> V componentExtract(HashMap<K, V> container, K searchKey, V defaultVal) {
        if (!container.containsKey(searchKey)) {
            container.put(searchKey, defaultVal);
        }
        return container.get(searchKey);
    }

}
